package com.enjoy.ch1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 自定义类加载器
 */
public class EnjoyClassloader extends ClassLoader {

    @Override
    protected Class<?> findClass(String name) {
        // 文件的二进制数据
        byte[] data = loadByte(name);
        return defineClass(name, data, 0, data.length);
    }

    private byte[] loadByte(String name) {
        String s = "F:/" + name.replaceAll("\\.", "/") + ".class";
        File file = new File(s);
        int available = 0;
        FileInputStream fileInputStream = null;
        byte[] bytes = null;
        try {
            fileInputStream = new FileInputStream(file);
            available = fileInputStream.available();
            bytes = new byte[available];
            int read = fileInputStream.read(bytes);
            System.out.println(read);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileInputStream != null;
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    @Override
    protected Class<?> loadClass(String name, boolean resolve) {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (name.contains("com.enjoy")) {
                        c = findClass(name);
                    } else {
                        c = getParent().loadClass(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }


    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        ClassLoader classLoader = new EnjoyClassloader();
        Class<?> aClass = classLoader.loadClass("com.enjoy.ch1.A");
        Method xx = aClass.getDeclaredMethod("xx");
        Object o = aClass.newInstance();
        xx.invoke(o);
        System.out.println(o.getClass().getClassLoader());
    }
}
